﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMSModel;
using MainQuoteDBModel;

public partial class Topics_scrd_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bd();
        }
    }

    private void bd()
    {
        string typeId = Request["typeId"];
        CMSEntities cms = new CMSEntities();
        var id = Guid.Parse(Request["id"]);
        string content= cms.Topics.Where(x=>x.ID==id).Select(x => x.Content).First();
        var t = Newtonsoft.Json.JsonConvert.DeserializeObject<Topics>(content);
        txtContent.Text = t.News.Content;
        string stocks = "";
        foreach (var c in t.Stocks)
        {
            stocks += c.Stock + ",";
        }
        if (!string.IsNullOrEmpty(stocks))
        {
            stocks = stocks.Substring(0, stocks.Length - 1);
        }
        txtStocks.Text = stocks;
        txtTitle.Text = t.News.Title;
        txtUrl.Text = t.News.Url;
        Image1.ImageUrl = "~/file/scjd/" + t.News.ImgUrl;
        ddlGZD.SelectedValue = t.News.GZD.ToString();
        ddlYXL.SelectedValue = t.News.YXL.ToString();
        HiddenField1.Value = t.News.ImgUrl;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        CMSEntities cms = new CMSEntities();
        string typeId = Request["typeId"];
        var id = Guid.Parse( Request["id"]);
        var data = cms.Topics.First(x => x.ID == id);
        Topics t = new Topics();
        t.News = new TopicsNews();
        t.News.Content = txtContent.Text;
        t.TypeId = Request["typeid"];
        t.TypeName = cms.TopicsType.First(x => x.ID == t.TypeId).Name;
        t.News.GZD = int.Parse(ddlGZD.SelectedValue);
        t.News.YXL = int.Parse(ddlYXL.SelectedValue);
        t.News.Url = txtUrl.Text;
        var stocks = txtStocks.Text.Split(',', '，');
        t.Stocks = new List<TopicsStock>();
        t.News.ImgUrl = HiddenField1.Value;
        t.News.Title = txtTitle.Text;
        MainQuoteDBEntities stockDAL = new MainQuoteDBEntities();
        foreach (var c in stocks)
        {
            TopicsStock stock = new TopicsStock();
            if (string.IsNullOrEmpty(c))
            {
                continue;
            }
            stock.Stock = c.Trim();
            stock.StockName = CommonBll.DicStocks[c];
            stock.HYBH = stockDAL.TB_BK_GG.Where(x => x.GG_CODE == stock.Stock).First().BK_CODE;
            var hymcs = stockDAL.TB_BK.Where(x => x.BK_CODE == stock.HYBH);
            if(stockDAL.TB_BK.Any(x => x.BK_CODE == stock.HYBH && x.BK_CODE.StartsWith("001")))
            {
                stock.HYMC = stockDAL.TB_BK.Where(x => x.BK_CODE == stock.HYBH && x.BK_CODE.StartsWith("001")).First().BK_NAME;
            }
            t.Stocks.Add(stock);
        }
        data.Content = Newtonsoft.Json.JsonConvert.SerializeObject(t);
        cms.SaveChanges();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string path = Server.MapPath("~/file/scjd/");
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            string houzui = "";
            if (FileUpload1.FileName.IndexOf(".") > -1)
            {
                houzui = FileUpload1.FileName.Substring(FileUpload1.FileName.IndexOf("."));
            }
            string file = Guid.NewGuid().ToString() + houzui;
            string filePath = path + file;
            FileUpload1.SaveAs(filePath);
            HiddenField1.Value = file;
            Image1.ImageUrl = "~/file/scjd/" + file;
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('上传成功！')", true);
        }
    }
}